package com.bzoa.api.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bzoa.api.dto.UserDTO;
import com.bzoa.api.dto.UserListPageDTO;
import com.bzoa.api.service.UserService;
import com.bzoa.api.entity.User;
import com.bzoa.api.mapper.UserMapper;
import com.springboot.common.entity.ResponseResult;
import com.springboot.common.entity.ResponseStatus;
import com.springboot.common.util.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
/**
 * @Author lands
 * @Date 2023/3/6 10:32
 * @Description  用户业务实现类
 */
@Service
public class UserServiceImp extends ServiceImpl<UserMapper,User> implements UserService  {
    @Override
    public User getUser(String id) {
        return  this.baseMapper.getUser(id);
    }
    @Override
    public ResponseResult getUserListPage(int page, int pageSize,String mobile,String nickname) {
//        QueryWrapper<UserListPageDTO> wrapper = new QueryWrapper<>();
//        wrapper.like("mobile",mobile).like("","");
        return ResponseResult.ok(this.baseMapper.getUserListPage((page-1)*pageSize,pageSize, mobile,nickname));
    }
    @Override
    public ResponseResult login(String username, String pwd) {

        User user = this.baseMapper.getUserByUsernamePwd(username,pwd);
        UserDTO userDTO = new UserDTO();
        if(user!=null){
            String token = JwtUtils.getJwtToken(user.getId(),user.getRole());
            BeanUtils.copyProperties(user,userDTO);
            HashMap<String,Object> map = new HashMap<>();
            map.put("token",token);
            map.put("user",userDTO);
            return ResponseResult.ok(map) ;
        }
        return ResponseResult.error(ResponseStatus.LOGIN_ERROR);
    }
    @Override
    public ResponseResult regist(String username, String pwd) {
        User user = new User();
        user.setPassword(pwd);
        user.setUsername(username);
        return ResponseResult.error("还没做");
    }
}
